﻿using System;
using System.Collections.Generic;
using System.Linq;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class SupplierBiz
    {
        #region Properties

        private readonly ISupplierGroupDao _supplierGroupDao;
        private readonly ISupplierDao _supplierDao;

        #endregion

        #region Constructors

        public SupplierBiz()
        {
            _supplierGroupDao = new SupplierGroupDao();
            _supplierDao = new SupplierDao();
        }

        public SupplierBiz(ISupplierGroupDao supplierGroupDao, ISupplierDao supplierDao)
        {
            _supplierGroupDao = supplierGroupDao;
            _supplierDao = supplierDao;
        }

        #endregion

        #region Methods

        public List<SupplierGroup> GetAllSupplierGroups()
        {
            try
            {
                return _supplierGroupDao.GetAllSupplierGroups();
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
            return new List<SupplierGroup>();
        }

        public Supplier GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _supplierDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Supplier CreateSupplier(Supplier supplier)
        {
            if (supplier == null) return null;
            if (string.IsNullOrEmpty(supplier.Code) || string.IsNullOrEmpty(supplier.Name)) return null;

            try
            {
                var addedSupplier = _supplierDao.AddSupplier(supplier);
                return addedSupplier;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public Supplier UpdateSupplier(Supplier supplier)
        {
            if (supplier == null) return null;

            try
            {
                _supplierDao.UpdateSupplier(supplier);
                return supplier;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _supplierDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _supplierDao.GetCurrentIdentity() + 1;
        }

        public object GetSupplierGroupOptionsWs()
        {
            try
            {
                var groups = _supplierGroupDao.GetAllSupplierGroups();//.Select(g => new { DisplayText = g.Name, Value = g.Id });
                groups.Insert(0, new SupplierGroup { Id = 0, Name = "" });
                return new { Result = "OK", Options = groups.Select(g => new { DisplayText = g.Name, Value = g.Id }) };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public int CountSuppliers(string name, int supplierGroupId)
        {
            return _supplierDao.GetSupplierCountByFilter(name, supplierGroupId);
        }

        public IList<Supplier> SearchSuppliers(string name, int supplierGroupId, int jtStartIndex, int jtPageSize,
            string jtSorting)
        {
            return _supplierDao.GetSuppliersByFilter(name, supplierGroupId, jtStartIndex, jtPageSize, jtSorting);
        }

        public object SupplierListByFilterWs(string name, int supplierGroupId, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                var supplierCount = _supplierDao.GetSupplierCountByFilter(name, supplierGroupId);
                var suppliers = _supplierDao.GetSuppliersByFilter(name, supplierGroupId, jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = suppliers, TotalRecordCount = supplierCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateSupplierWs(Supplier supplier)
        {
            if (supplier == null) return null;
            if (string.IsNullOrEmpty(supplier.Code) || string.IsNullOrEmpty(supplier.Name)) return null;

            try
            {
                var addedSupplier = _supplierDao.AddSupplier(supplier);
                return new { Result = "OK", Record = addedSupplier };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateSupplierWs(Supplier supplier)
        {
            if (supplier == null) return null;

            try
            {
                _supplierDao.UpdateSupplier(supplier);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteSupplierWs(int supplierId)
        {
            try
            {
                _supplierDao.DeleteSupplier(supplierId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers
        #endregion
    }
}
